import { defineStore } from 'pinia';
import { request } from '@/utils'
import { ref, computed } from 'vue'
export const useStore = defineStore('user', () => {
	const token = ref(uni.getStorageSync('token') ?? '')
	const myInfo = ref({})
	const isLogin = computed(() => {
		let retn = false
		if (token.value) {
			retn = true
		}
		return retn
	})

	const login = async (data) => {
		const res = await request('/api/v1/loginApp', data, 'POST')
		if (res.data.code !== 1000) {
			// uni.showToast({
			// 	title: res.data.message,
			// 	icon: 'error'
			// })
			return res
		} else {
			uni.setStorageSync('token', res.header['api-token'])
			token.value = res.header['api-token']
			return res
		}
	}
	
	const logout = async () => {
		uni.removeStorageSync('token')
		token.value = ''
		uni.reLaunch({
			url: '/pages/login/login'
		})
	}
	
	const getMyInfo = async () => {
		const res = await request('/api/v1/getMyInfo', {}, 'GET')
		myInfo.value = res.data
	}

	return {token, myInfo, isLogin, login, logout, getMyInfo };
});